package com.huxun.controller;

import com.huxun.bean.Cart;
import com.huxun.bean.Desk;
import com.huxun.bean.Food;
import com.huxun.bean.Foodtype;
import com.huxun.service.CartService;
import com.huxun.service.FoodService;
import com.huxun.service.FoodTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    FoodService foodService;
    @Autowired
    FoodTypeService foodTypeService;
    @RequestMapping("/cart_add/{food_id}")
    public void addCart(@PathVariable("food_id")int food_id, HttpServletResponse response, HttpSession session) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        Desk desk = (Desk) session.getAttribute("desk");
        //判断购物车中是否菜品数据
        Map<String, Object> map = new HashMap<>();
        map.put("desk_id",desk.getId());
        map.put("food_id",food_id);
        int count = cartService.isExists(map);
        Food food = foodService.getById(food_id);
        if (count>0){
            Cart cart = cartService.getMyCart(map);
            cart.setNum(cart.getNum()+1);
            cart.setTotal(cart.getNum()*food.getPrice());
            cartService.updateCart(cart);
            System.out.println("购物车数量更新啦！");
        }else {
            Cart cart = new Cart();
            cart.setNum(1);
            cart.setDesk(desk);
            cart.setFood(food);
            cart.setTotal(cart.getNum()*food.getPrice());
            cartService.addCart(cart);
            System.out.println("新增购物车啦！");
        }
        response.getWriter().write("<script>alert('加入餐车成功!,请继续点餐。');location.href='/menu_ui/"+desk.getId()+"/1';</script>");
    }
    @RequestMapping("/my_carts")
    public String mycarts(HttpSession session, Model model){
        Desk desk = (Desk) session.getAttribute("desk");
        //所有的菜系信息
        List<Foodtype> foodTypes = foodTypeService.getAllFoodTypes();
        model.addAttribute("foodTypes",foodTypes);
        List<Cart> carts = cartService.getCarts((int) desk.getId());
        model.addAttribute("carts",carts);
        double sum=0;
        for (Cart cart : carts) {
            sum=cart.getTotal()+sum;
        }
        model.addAttribute("sum",sum);
        return "clientCart";
    }

    @RequestMapping("/updateCartNumber/{cart_id}/{snumber}")
    public void updateCartNumber(@PathVariable("cart_id")int cart_id,@PathVariable("snumber")int snumber,HttpServletResponse response)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        Cart cart = cartService.getById(cart_id);
        cart.setNum(snumber);
        cart.setTotal(cart.getFood().getPrice()*cart.getNum());
        int count = cartService.updateCart(cart);
        if (count>0){
            response.getWriter().write("<script>alert('修改数量成功！');location.href='/my_carts';</script>");
        }else {
            response.getWriter().write("<script>alert('修改数量失败！');location.href='/my_carts';</script>");
        }
    }
    @RequestMapping("/deleteCart/{cart_id}")
    public void deleteCart(@PathVariable("cart_id")int cart_id,HttpServletResponse response)throws Exception{
        response.setContentType("text/html;charset=utf-8");
        int count = cartService.deleteCartById(cart_id);
        if (count>0){
            response.getWriter().write("<script>alert('删除成功！');location.href='/my_carts';</script>");
        }else {
            response.getWriter().write("<script>alert('删除失败！');location.href='/my_carts';</script>");
        }
    }
}
